package com.controller;

import com.dao.BGoodDao;
import com.dao.BTypeDao;
import com.model.BGood;
import com.model.BType;
import com.model.FUser;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {
    @Resource
    BTypeDao bTypeDao;

    @Resource
    BGoodDao bGoodDao;
    //前台首页
    @GetMapping(value = "/")
    public String index(ModelMap model, HttpServletRequest request){
        //定义字典 key：商品分类名称，value：对应分类下的商品列表
        Map<String, List<BGood>> map =new LinkedHashMap<>();
        //获取商品类型列表
         List<BType> typeList =bTypeDao.typeList();
         //遍历列表，读取对应商品类型的商品数据
         for(BType bType:typeList){
             //根据商品类型读取商品数据
             List<BGood> goodList=bGoodDao.getGoodByTypeId(bType.getId());
             //写入到字典
             map.put(bType.getType_name(),goodList);
         }
         HttpSession session = request.getSession();
         FUser user =(FUser) session.getAttribute("USER_SESSION");
         model.addAttribute("user",user);
         model.addAttribute("typeList",typeList);
         model.addAttribute("list",map);
         return "front/index";
    }
}
